#define SPLINE
//#define MAP

using System;
using System.Collections.Generic;
using System.Text;
using IrrlichtNETCP;
using IrrlichtNETCP.Inheritable;

namespace Tubes {
    class csMake3DModelScene : irrControl.ISceneContainer {
        private IrrlichtDevice device;
        public bool setup(IrrlichtDevice device) {

            this.device = device;
            // setup a simple 3d scene
            SceneManager smgr = device.SceneManager;
            VideoDriver driver = device.VideoDriver;

            CameraSceneNode cam = smgr.AddCameraSceneNodeMaya(null, 100.0f, 200.0f, 80.0f, -1);
            cam.Position = new Vector3D(0, 5, -10);
            cam.Target = new Vector3D(0, 0, 0);
            cam.FOV = (float)(Math.PI / 4.0);
            cam.NearValue = 0.01f;
            cam.FarValue = 1000;
            cam.InputReceiverEnabled = true;


            LightSceneNode l = smgr.AddLightSceneNode(cam, new Vector3D(0, 10, 0), Colorf.White, 8f, -1);
            LightSceneNode l1 = smgr.AddLightSceneNode(cam, new Vector3D(0, -10, 0), Colorf.White, 8.0f, -1);

            string texture_name = "";


#if SPLINE            
            int spline_len = 800000;
            int circle_sides = 20;
            float circle_radius = 0.2f;
                                            
            csMake3D.makers.SplineExtruder spline = new csMake3D.makers.SplineExtruder();            
                for (int j = 0; j < spline_len; j++) {
                    float tm = 2.0f * (float)Math.PI * (float)j / (float)spline_len;
                    float x = (float)( Math.Cos(tm));
                    float y = (float)( Math.Sin(tm));
                    float z = (float)( Math.Sin(2*tm));
                    spline.Spline.Add(new Sharp3D.Math.Core.Vector3F( 2.0f*x, 2.0f*y, 2.0f*z));
                }

                spline.Section = csMake3D.makers.ShapeMaker.makeRegularPolygon(circle_sides, circle_radius);

                spline.Section = csMake3D.makers.ShapeMaker.makeBotanicShape(3, -2, circle_sides, circle_radius);
                spline.Spline = csMake3D.utils.helpers.Manipulators.OptimizeSpline(spline.Spline, circle_radius);                            
                spline.Scale.Add( 1);
                spline.Scale.Add( 0.5f);
                spline.Scale.Add( 1);


                spline.Rotation.Add(0);
                spline.Rotation.Add((float)Math.PI);
                spline.Rotation.Add((float)Math.PI*2.0f);

            csMake3D.core.Vertex[] ex = spline.getExtrusion();
            int [] indices =  spline.getIndices();

            csMake3D.utils.helpers.NormalsMapping.calcSmoothNormals(ref ex, ref indices );
                
            csMake3D.utils.helpers.TexturesMapping.SphericalTextureMapping(ref ex, csMake3D.utils.helpers.TexturesMapping.Alignment.XZ);

            Console.WriteLine("Optimization From #{0} to #{1} points.", spline_len, spline.Spline.Count);               
            
            csMake3D.core.Model m = new csMake3D.core.Model(ex, spline.getIndices());               
            Animator anim =
            smgr.CreateRotationAnimator(new Vector3D(0, 1f, 0));

            texture_name = "data/tex.jpg";
#else
            /* csMake3D.makers.TerrainMaker tm = new csMake3D.makers.TerrainMaker(csMake3D.makers.MathFunctions.sombrero, new Sharp3D.Math.Core.Vector2F(-2.0f * (float) Math.PI, -2.0f * (float)Math.PI),
                                                             new Sharp3D.Math.Core.Vector2D(2*Math.PI,2*Math.PI),
                                                             new Sharp3D.Math.Core.Vector2F((float)Math.PI/20.0f,(float)Math.PI/20.0f));
            
            */

            csMake3D.makers.TerrainMaker tm = new csMake3D.makers.TerrainMaker(".\\data\\heightmap.bmp", 10, 1f);
            csMake3D.core.Vertex[] ex = tm.getVertices();              
            csMake3D.core.Model m = new csMake3D.core.Model(ex, tm.getIndices());

            Animator anim = smgr.CreateRotationAnimator(new Vector3D(0, 0.3f, 0));

            texture_name = "data/heightmap_tex.jpg";
#endif


            ModelSceneNode t = new ModelSceneNode(null, -1, device.SceneManager, m);


            //csMake3D.utils.io.WavefrontFileWriter.save("c:\\Modelli\\Modello.obj", m , ".\\data\\tex.jpg");
            csMake3D.utils.io.VRMLFileWriter.save("c:\\Modelli\\Modello.wrl", texture_name, m);

            
            t.AddAnimator(anim);
            anim.Dispose();

            t.GetMaterial(0).Lighting = true;
            t.GetMaterial(0).Texture1 = driver.GetTexture(texture_name);
            t.GetMaterial(0).MaterialType = MaterialType.Solid;           
            return true;
        }


        public void render(float dt) {
            device.SceneManager.DrawAll();
        }

        public void dismiss() {

        }
        public bool onEvent(IrrlichtNETCP.Event evt) {
            return false;
        }

        public void refreshViewport(int w, int h) {

            CameraSceneNode cam = device.SceneManager.ActiveCamera;
            cam.AspectRatio = w / (float)(h);

        }

        public IrrlichtNETCP.Color BackColor {
            get { return IrrlichtNETCP.Color.Gray; }
            set { ;}
        }
    }
}
